package com.ihr360.insurance.controller.ledger;

import com.ihr360.authority.sharing.annonation.ControlMode;
import com.ihr360.authority.sharing.annonation.RestControl;
import com.ihr360.authority.sharing.privilege.AccessControlMethod;
import com.ihr360.commons.data.util.Pair;
import com.ihr360.commons.model.type.SIHFLedgerState;
import com.ihr360.entity.ApplicationApprovalEntity;
import com.ihr360.insurance.entity.request.ApplicationIdBenefitRequest;
import com.ihr360.insurance.entity.response.BenefitApprovalListResponse;
import com.ihr360.insurance.model.dto.ledger.MonthlyLedgerSummary;
import com.ihr360.insurance.service.MonthlyLedgerSummaryService;
import com.ihr360.maven.plugin.ModuleCodeStore;
import com.ihr360.maven.plugin.annotation.FunctionDefinition;
import com.ihr360.maven.plugin.annotation.ModuleDefinition;
import com.ihr360.rest.service.SimpleRestService;
import com.ihr360.rest.webmvc.AbstractRestSimpleWithLongIdController;
import com.ihr360.shared.advice.response.DisableResponseAdvice;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 社保公积金台账概览
 *
 * @author david.wei
 */
@RestController
@RequestMapping("/monthlyLedgerSummaries")
@ModuleDefinition(mainModuleCode = ModuleCodeStore.Module.BENEFIT_CODE,
        mainModuleName = ModuleCodeStore.Module.BENEFIT_NAME,
        subModuleCode = ModuleCodeStore.Module.Benefit.STANDING_BOOK_CODE,
        subModuleName = ModuleCodeStore.Module.Benefit.STANDING_BOOK_NAME,
        hasExtendsAbstractRestController = true)
@RestControl(mode = ControlMode.ALLOW,
        controls = {AccessControlMethod.METHODS.deleteOne, AccessControlMethod.METHODS.search})
public class MonthlyLedgerSummaryController extends AbstractRestSimpleWithLongIdController<MonthlyLedgerSummary> {
    @Autowired
    private MonthlyLedgerSummaryService summaryService;

    @Override
    public SimpleRestService getSimpleRestService() {
        return summaryService;
    }

    /**
     * feign接口
     */
    @FunctionDefinition(functionName = "获取台账所属的用户", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/idUserId", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @DisableResponseAdvice
    public Pair<Long, String> getLedgerSummaryIdUserId(@RequestParam String companyId,
                                                       @RequestParam String applicationId) {
        return summaryService.getLedgerSummaryUserId(companyId, applicationId);
    }

    @FunctionDefinition(functionName = "台账是否存在", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/isExisted", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean isLedgerExisted(@RequestParam String companyId,
                                   @RequestParam String userId,
                                   @RequestParam Integer year,
                                   @RequestParam Integer month) {
        return summaryService.isLedgerExisted(companyId, userId, year, month);
    }

    @FunctionDefinition(functionName = "台账是否锁定", functionCode = ModuleCodeStore.Function.VIEW)
    @RequestMapping(value = "/isLocked", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public boolean isLedgerLocked(@RequestParam String companyId,
                                  @RequestParam String userId,
                                  @RequestParam Integer year,
                                  @RequestParam Integer month) {
        return summaryService.isLedgerLocked(companyId, userId, year, month);
    }


    /**
     * 根据审批applicationId查询账期信息
     * */
    @FunctionDefinition(functionName = "根据审批applicationId查询账期信息", functionCode = ModuleCodeStore.Function.VIEW)
    @ApiOperation(value = "根据审批applicationId查询账期信息")
    @PostMapping(value = "/benefit/approval/list")
    @DisableResponseAdvice
    public BenefitApprovalListResponse queryApprovalBenefit(@RequestBody ApplicationIdBenefitRequest request) {
        BenefitApprovalListResponse response = new BenefitApprovalListResponse();
        response.setBenefitApprovalResponseList(summaryService.queryApprovalBenefit(request.getCompanyId(),request.getApplicationIdList()));
        return response;
    }

    /**
     * 提请审批保存审批id，状态
     * */
    @FunctionDefinition(functionName = "提请审批保存审批id", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "提请审批保存审批id，状态")
    @PostMapping(value = "/benefit/submit/approval")
    public void saveApprovalApplicationId(@RequestParam String companyId, @RequestBody ApplicationApprovalEntity entity, @RequestParam Long id) {
        summaryService.saveApprovalApplicationId(companyId,entity,id);
    }

    /**
     * 审批同步修改账期状态
     * */
    @FunctionDefinition(functionName = "审批同步修改账期状态", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "审批同步修改账期状态")
    @GetMapping(value = "/benefit/approval/state")
    public void updateApprovalState(@RequestParam String companyId, @RequestParam String applicationId, @RequestParam SIHFLedgerState state) {
        summaryService.updateApprovalState(companyId,applicationId,state);
    }

    /**
     * 福利账期关账
     */
    @FunctionDefinition(functionName = "福利账期关账", functionCode = ModuleCodeStore.Function.UPDATE)
    @ApiOperation(value = "福利账期关账")
    @GetMapping(value = "/benefit/close")
    public void closeLedgerSummary(@RequestParam Long id) {
        summaryService.closeLedgerSummary(id);
    }
}